package com.starblink.android.basic.data

import com.blankj.utilcode.util.GsonUtils
import com.starblink.android.basic.config.StorageKeys
import com.starblink.basic.storage.SkStorage
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import java.util.*

object SearchDataManager {

    fun saveNormalWord(word: String) {
        MainScope().async {
            val history = LinkedList<String>()
            val key = StorageKeys.SEARCH_HISTORY
            GsonUtils.fromJson(
                SkStorage.getString(key, ""),
                history.javaClass
            )?.let {
                history.addAll(it)
            }
            if (history.first != word) {
                if (history.contains(word)) {
                    history.remove(word)
                }
                if (history.size > 15) {
                    history.removeLast()
                }
                history.add(0, word)
                SkStorage.put(key, GsonUtils.toJson(history))
            }
        }
    }

}